<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>生成器函数-generator</div>
    <div>是一个带星号的函数，可以暂停执行（yield）和恢复执行（next()）</div>
  </body>
  <script src="./co/co.js"></script>
  <script>
    function* demoFn() {
      console.log('开始执行第一段')
      yield 'generator 1'
      console.log('开始执行第二段')
      yield 'generator 2'
      console.log('开始执行第三段')
      yield 'generator 3'
      console.log('开始执行第四段')
      yield 'generator 4'
      console.log('执行结束')
      return 'generator-end'
    }
    // let gen = demoFn()
    // console.log('main-1')
    // console.log(gen.next().value)
    // console.log('main-2')
    // console.log(gen.next().value)
    // console.log('main-3')
    // console.log(gen.next().value)
    // console.log('main-4')
    // console.log(gen.next().value)
    // console.log('main-5', gen)
    // console.log(gen.next().value)

    let request = (name, ms = 2000) => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ name })
        }, ms)
      })
    }
    function* getData() {
      let name1 = yield request('cc')
      console.log('name1==', name1) // {name:'cc'}
      let name2 = yield request(name1)
      console.log('name2==', name2) // {name:{name:'cc'}}
      return name2
    }

    // let p0 = co(getData())
    // console.log('p0', p0) // Promise
    // p0.then((res) => console.log(res)) // {name:{name:'cc'}}

    // let gen = getData()
    // gen.next().value.then((res) => {
    //   gen.next(res).value.then((res2) => {
    //     let tt = gen.next(res2).value
    //     console.log(tt) // {name:{name:'cc'}}
    //   })
    // })

    // async function foo() {
    //   console.log(1)
    //   let a = await 100
    //   console.log(a)
    //   console.log(2)
    // }
    // console.log(0)
    // let ff = foo()
    // console.log('ff==', ff)
    // console.log(3)

    async function foo() {
      console.log('foo')
    }
    async function bar() {
      console.log('bar start')
      await foo()
      console.log('bar end')
    }
    console.log('script start')
    setTimeout(function () {
      console.log('setTimeout')
    }, 0)
    bar()
    new Promise(function (resolve) {
      console.log('promise executor')
      resolve()
    }).then(function () {
      console.log('promise then')
    })
    console.log('script end')
  </script>
</html>
